import { defineStore } from "pinia";
import type { UserState } from './type/type'
// //引入路由(常量路由)
import { constantRoute } from '@/router/routes'
import { reqLogin } from "@/api/user";
import type {
    loginFormData
  } from '@/api/user/type'
// 创建用户小仓库
const useUserStore = defineStore("User", {
    // 小仓库存储数据
    state:():UserState => {
        return {
            token:localStorage.getItem("TOKEN"), //用户唯一标识token
            menuRoutes: constantRoute, //仓库存储生成菜单需要数组(路由)
            username:"username"
        }
    },
    actions:{
        async userLogin(data:loginFormData){
            console.log("denglu")
            const result : any = await reqLogin(data)
            if (result.status == 100){
              this.token = result.token as string
              this.username = result.username as string
              localStorage.setItem("TOKEN",this.token)
              console.log(this.token)
              return "ok"
      
            } else {
              return Promise.reject(new Error("登录失败1"))
            }
          },
          loginOut(){
            this.token='';
            this.username='';
            localStorage.removeItem("TOKEN")
      
          }
    },
    getters:{

    }
})
export default useUserStore